﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebLibSolPedBE;

namespace WebApplication1
{
    public partial class formReservaLista : System.Web.UI.Page
    {
        List<ReservaBE> lst = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["nombreApp"] = "libreserva";
                if (Session["objUser"] == null)
                {
                    Response.Redirect("credencial.aspx");
                }
                else
                {
                    ModalPopupExtender1.Hide();
                    CargarLista();
                }
            }
            else
            {
                string acci = Session["accion"] as string;
                if (acci.Equals("R"))
                {
                    CargarLista();
                    Session["accion"] = "";
                }
            }
        }

        private void CargarLista()
        {
            List<ReservaBE> lst = LibSolPedDALF.DaoFactory.LiberaReservaF.getLstReserva();
            gvGrid.DataSource = lst;
            gvGrid.DataBind();
            Session["lstReserva"] = lst;
        }

        protected void gvGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView gv = (GridView)sender;
            int filaSeleccionada = gv.SelectedIndex;
            lst = (List<ReservaBE>)Session["lstReserva"];
            try
            {
                Session.Add("objReserva", lst[filaSeleccionada]);
                Response.Redirect("formReservaDetalle.aspx");
            }
            catch (Exception ex)
            {

            }
        }

        protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Find the control
                var lblRsposRsNum = (Label)e.Row.FindControl("lblRsposRsNum");
                var lblMaktx = (Label)e.Row.FindControl("lblMaktx");
                var lblWerks = (Label)e.Row.FindControl("lblWerks");
                var lblUsnam = (Label)e.Row.FindControl("lblUsnam");
                var lblKostl = (Label)e.Row.FindControl("lblKostl");
                //Display the data
                var data = e.Row.DataItem;
                ReservaBE it = (ReservaBE)data;
                lblRsposRsNum.Text = it.RSPOS + "-" + it.RSNUM;
                lblMaktx.Text = it.MAKTX;
                lblWerks.Text = it.WERKS;
                lblUsnam.Text = it.USNAM;
                lblKostl.Text = it.KOSTL;
            }
        }

        protected void gvGrid_RowCreated(Object sender, GridViewRowEventArgs e)
        {

            // The GridViewCommandEventArgs class does not contain a
            // property that indicates which row's command button was
            // clicked. To identify which row was clicked, use the button's
            // CommandArgument property by setting it to the row's index.
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Retrieve the LinkButton control from the first column.
                LinkButton addButton = (LinkButton)e.Row.Cells[2].Controls[2].FindControl("LinkButton1");

                // Set the LinkButton's CommandArgument property with the
                // row's index.
                addButton.CommandArgument = e.Row.RowIndex.ToString();
            }

        }
        protected void gvGrid_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                GridView gv = (GridView)sender;
                int filaSeleccionada = Convert.ToInt32(e.CommandArgument);// gv.SelectedIndex;
                lst = (List<ReservaBE>)Session["lstReserva"];
                try
                {
                    Session.Add("objReserva", lst[filaSeleccionada]);
                }
                catch (Exception ex) { }

                string url = String.Format("formReservaDetalle.aspx");
                Response.Redirect(url);
            }
        }

    }
}